package com.bootdo.shop.dao;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.bootdo.shop.dao.provider.GoodsMapperProvider;
import com.bootdo.shop.domain.Category;
import com.bootdo.shop.domain.Goods;
import com.bootdo.shop.domain.GoodsCategory;
import com.bootdo.shop.domain.GoodsImage;
import com.bootdo.shop.domain.GoodsLimit;
import com.bootdo.shop.domain.GoodsSortConfig;
import com.bootdo.shop.domain.PostageConfig;
import com.bootdo.shop.domain.SystemCity;
import com.bootdo.shop.domain.request.GoodsReq;
import com.bootdo.shop.domain.response.GoodsRes;



/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2015年12月30日 下午1:27:05 
 * 类说明 
 */
public interface GoodsMapper {
	
	@Update("update goods set amount = amount - #{num} where id = #{goodsId}")
	public void subtractStock(@Param("goodsId") Integer goodsId,@Param("num") Integer num);
	
	@Select("select * from goods where id = #{goodsId}")
	public Goods getGoodsById(Integer goodsId);
	
	
	@Update("update goods set amount = amount + #{num} where id = #{goodsId}")
	public void increaseStock(@Param("goodsId") Integer goodsId,@Param("num") Integer num);
	
	@Update("update goods set buy_count = (select (goods.buy_count + c.buy_count) from "
			+ " ( select sum(b.goods_number) buy_count,b.goods_id "
			+ " from order_info a,order_detail b where a.id = b.order_id and a.order_no "
			+ "			= #{orderNo} and b.is_service != 1  group by b.goods_id ) c where "
			+ " goods.id = c.goods_id ) where (select * from ( select b.goods_id from order_info a,order_detail b "
			+ " where a.id = b.order_id and a.order_no = #{orderNo} and b.is_service != 1 group "
			+ " by b.goods_id) c where goods.id = c.goods_id )")
	public Integer updateGoodsBuyCountByOrderNo(String orderNo);
	
	@Select("select count(*) from order_detail b,order_info a where a.id=b.order_id and b.is_service != 1")
	public Integer countBuyGoods(String orderNo);
	
	@Update("update order_info set is_comment=1 where id = #{orderId}")
	public Integer commentedOrder(Integer orderId);
	
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsHomeList")
	public List<GoodsRes> getHomeList(GoodsReq goodsReq);
	
	@Select("select * from goods where id = #{id}")
	public GoodsRes getGoodsDetailById(Integer id);
	
	@Select("select * from goods_image where goods_id = #{goodsId} and status = 1 order by sort_order asc")
	public List<GoodsImage> getGoodsImageList(Integer goodsId);
	
	@Select("select * from t_system_city order by seq asc")
	public List<SystemCity> getAllRegion();
	
	/**
	 * 根据省ID获取邮费配置信息
	 * @param provinceId
	 * @return
	 */
	@Select("select * from postage_config where province_id = #{provinceId}")
	public PostageConfig getPostageConfigByProvinceId(Integer provinceId);
	
	/**
	 * 只用大于100的ID，小于100的用于自定义
	 * @return
	 */
	@Select("select * from category where id > 100 and status = 1  order by sort_order desc")
	public List<Category> categoryList();
	
	@Select("select * from category where id = #{id}")
	public Category getCategoryById(Integer id);
	
	@Select("select * from category where id > 100  order by sort_order desc")
	public List<Category> categoryListByManager();
	
	@Select("select id,name,seq from goods_sort_config order by seq desc")
	public List<GoodsSortConfig> sortList();
	
	@Select("select id,name,seq,config from goods_sort_config where id = #{id}")
	public GoodsSortConfig getGoodsSortConfig(Integer id);
	/**
	 * 没有搜到商品推荐
	 * @return
	 */
	@SelectProvider(type=GoodsMapperProvider.class,method="notSearchrecommendGoodsList")
	public List<GoodsRes> notSearchrecommendGoodsList();
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsList")
	public List<GoodsRes> goodsList(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsListCount")
	public Integer goodsListCount(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="medtronicGoodsList")
	public List<GoodsRes> medtronicGoodsList(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="marketingActivityGoodsList")
	public List<GoodsRes> marketingActivityGoodsList(GoodsReq goodsReq);
	
	@Select("select b.id,title,b.market_price,b.sale_price,b.is_hot,b.is_recommend,b.thumbnail_image "
			+ "from goods_recommend a ,goods b where a.recommend_id = b.id and a.goods_id = #{goodsId}")
	public List<GoodsRes> detailRecommend(Integer goodsId);
	
	@Select("select count(1) from t_stockout_log where user_id = #{userId} and goods_id = #{goodsId} and is_send = 0")
	public Integer isStockoutCheckIn(@Param("userId") Integer userId,@Param("goodsId") Integer goodsId);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="getGoodsLimitList")
	public List<GoodsLimit> getGoodsLimitList(GoodsLimit goodsLimit);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="shoppingCartRecommend")
	public List<GoodsRes> shoppingCartRecommend(@Param("goodsList") List<Integer> goodsList);
	/**
	 * 校验商品是否纯服务
	 * @param goodsId
	 * @return
	 */
	@SelectProvider(type=GoodsMapperProvider.class,method="verifyGoodsIsService")
	public Map<String, Number> verifyGoodsIsService(@Param("goodsId") Integer goodsId,@Param("specificationId") Integer specificationId);
	
	@Select("select name from store_item where id = #{storeItemId}")
	public String getStoreItemName(Integer storeItemId);
	/**
	 * 修改分类
	 * @param category
	 * @return
	 */
	@UpdateProvider(type=GoodsMapperProvider.class,method="updateCategory")
	public Integer updateCategory(Category category);
	
	/**
	 * 修改分类
	 * @param category
	 * @return
	 */
	@UpdateProvider(type=GoodsMapperProvider.class,method="addCategory")
	public Integer addCategory(Category category);
	
	/**
	 * 修改商品
	 * @param goodsReq
	 * @return
	 */
	@UpdateProvider(type=GoodsMapperProvider.class,method="updateGoods")
	public Integer updateGoods(GoodsReq goodsReq);
	
	/**
	 * 添加商品
	 * @param goodsReq
	 * @return
	 */
	@InsertProvider(type=GoodsMapperProvider.class,method="addGoods")
	@Options(useGeneratedKeys=true)
	public Integer addGoods(GoodsReq goodsReq);
	/**
	 * 删除商品大图
	 * @param goodsId
	 * @param sortOrder
	 * @return
	 */
	@Delete("delete from goods_image where goods_id = #{goodsId} and sort_order = #{sortOrder}")
	public Integer deleteGoodsImage(@Param("goodsId")Integer goodsId,@Param("sortOrder")Integer sortOrder);
	/**
	 * 保存商品大图
	 * @param goodsId
	 * @param imageLarge
	 * @param sortOrder
	 * @return
	 */
	@Insert("insert into goods_image (goods_id,image_large,sort_order) values (#{goodsId},#{imageLarge}, #{sortOrder})")
	public Integer insertGoodsImage(GoodsImage goodsImage);
	/**
	 * 删除商品分类
	 * @param goodsId
	 * @return
	 */
	@Delete("delete from goods_category where goods_id = #{goodsId}")
	public Integer deleteGoodsCategoryByGoodsId(Integer goodsId);
	
	@Insert("insert into goods_category (goods_id,category_id) values (#{goodsId},#{categoryId})")
	public Integer insertGoodsCategory(@Param("goodsId")Integer goodsId,@Param("categoryId")Integer categoryId);
	
	@Select("select * from goods_category where goods_id = #{goodsId}")
	public List<GoodsCategory> getCategoryByGoodsId(Integer goodsId);
	
	@Select("select * from goods_limit where goods_id = #{goodsId}")
	public List<GoodsLimit> getGoodsLimit(Integer goodsId);
	
	@Delete("delete from goods_limit where goods_id = #{goodsId}")
	public Integer deleteGoodsLimit(Integer goodsId);
	
	@Insert("insert into goods_limit(goods_id,specification_id,limit_type,limit_count,remark) " + 
			"values " + 
			"(#{goodsId},0,#{limitType},#{limitCount},#{remark})")
	public Integer addGoodsLimit(GoodsLimit goodsLimit);
}